
/**
 * @ProjectName: 
 * @Copyright: 2014 lisheng  All Right Reserved.
 * @address: toughheart@163.com
 * @date: 2015年1月10日 下午3:04:11
 * @Description: 本内容未经本人允许禁止使用、转发.
 */
 
package com.ls.fw.search.lucene.bean;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;


/**
 * 搜索条件
 * @author lisheng
 * @date 2015年1月10日 下午3:04:11
 * @version V1.0 
 */
public class SearchCondition implements Serializable, Cloneable{

	
	/**
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
	 */
	private static final long serialVersionUID = 1L;
	private ScoreDoc lastPageLastDoc = null;
	private Query query = null;
	private Sort sort = null;
	private int maxDoc = 0;
	private boolean doMaxScore = false;
	private boolean doDocScores = false;
	
	private int offset = 0;
	private int page = 1;
	private int pageSize = 0;
	
	private Set<String> showFields = null;
	
	public SearchCondition() {
		super();
	}


	public SearchCondition(Sort sort, int maxDoc) {
		super();
		this.sort = sort;
		this.maxDoc = maxDoc;
	}
	
	
	public SearchCondition(Query query,  Sort sort, int maxDoc) {
		super();
		this.query = query;
		this.sort = sort;
		this.maxDoc = maxDoc;
	}
	
	
	public SearchCondition(ScoreDoc lastPageLastDoc, Query query,
			Sort sort, int maxDoc) {
		super();
		this.lastPageLastDoc = lastPageLastDoc;
		this.query = query;
		this.sort = sort;
		this.maxDoc = maxDoc;
	}
	public ScoreDoc getLastPageLastDoc() {
		return this.lastPageLastDoc;
	}
	public SearchCondition setLastPageLastDoc(ScoreDoc lastPageLastDoc) {
		this.lastPageLastDoc = lastPageLastDoc;
		return this;
	}
	public Query getQuery() {
		return this.query;
	}
	public SearchCondition setQuery(Query query) {
		this.query = query;
		return this;
	}
	public Sort getSort() {
		return this.sort;
	}
	public SearchCondition setSort(Sort sort) {
		this.sort = sort;
		return this;
	}
	public int getMaxDoc() {
		return this.maxDoc;
	}
	public SearchCondition setMaxDoc(int maxDoc) {
		this.maxDoc = maxDoc;
		return this;
	}
	
	public boolean isDoMaxScore() {
		return this.doMaxScore;
	}


	public SearchCondition setDoMaxScore(boolean doMaxScore) {
		this.doMaxScore = doMaxScore;
		return this;
	}


	public boolean isDoDocScores() {
		return this.doDocScores;
	}


	public SearchCondition setDoDocScores(boolean doDocScores) {
		this.doDocScores = doDocScores;
		return this;
	}
	
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("SearchCondition [lastPageLastDoc=");
		builder.append(this.lastPageLastDoc);
		builder.append(", query=");
		builder.append(this.query);
		builder.append(", sort=");
		builder.append(this.sort);
		builder.append(", maxDoc=");
		builder.append(this.maxDoc);
		builder.append(", doMaxScore=");
		builder.append(this.doMaxScore);
		builder.append(", doDocScores=");
		builder.append(this.doDocScores);
		builder.append(", offset=");
		builder.append(this.offset);
		builder.append(", page=");
		builder.append(this.page);
		builder.append(", pageSize=");
		builder.append(this.pageSize);
		builder.append("]");
		return builder.toString();
	}


	public int getOffset() {
		return this.offset;
	}


	public SearchCondition setOffset(int offset) {
		this.offset = offset;
		return this;
	}


	public int getPage() {
		return this.page;
	}


	public SearchCondition setPage(int page) {
		this.page = page;
		return this;
	}


	public int getPageSize() {
		return this.pageSize;
	}


	public SearchCondition setPageSize(int pageSize) {
		this.pageSize = pageSize;
		if(maxDoc==0){
			maxDoc = pageSize;
		}
		return this;
	}


	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}


	public Set<String> getShowFields() {
		return showFields;
	}


	public SearchCondition setShowFields(Set<String> showFields) {
		this.showFields = showFields;
		return this;
	}

	public SearchCondition addField(String name){
		if(showFields == null){
			showFields = new HashSet<String>();
		}
		showFields.add(name);
		return this;
	}
	
	public SearchCondition addField(Set<String> showFields){
		if(this.showFields == null){
			this.showFields = new HashSet<String>();
		}
		if(showFields!=null){
			this.showFields.addAll(showFields);
		}
		return this;
	}

	public String hashCodeStr() {
		return this.hashCode()+"";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (this.doDocScores ? 1231 : 1237);
		result = prime * result + (this.doMaxScore ? 1231 : 1237);
		result = prime * result + this.pageSize;
		result = prime * result
				+ ((this.query == null) ? 0 : this.query.hashCode());
		result = prime * result
				+ ((this.sort == null) ? 0 : this.sort.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SearchCondition other = (SearchCondition) obj;
		if (this.doDocScores != other.doDocScores)
			return false;
		if (this.doMaxScore != other.doMaxScore)
			return false;
		if (this.pageSize != other.pageSize)
			return false;
		if (this.query == null) {
			if (other.query != null)
				return false;
		} else if (!this.query.equals(other.query))
			return false;
		if (this.sort == null) {
			if (other.sort != null)
				return false;
		} else if (!this.sort.equals(other.sort))
			return false;
		return true;
	}
	
}
